package com.example.myapplication.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;

import java.util.ArrayList;

public abstract class MAdapter<T> extends BaseAdapter {
    private ArrayList<T> mData;
    private int mLayoutRes;

    public abstract void bindView(ViewHolder holder,T obj);
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.bind(parent.getContext(),convertView,parent,mLayoutRes,position);
        bindView(holder,getItem(position));
        return holder.getItemView();
    }

    public MAdapter(ArrayList<T> mData, int mLayoutRes) {
        this.mData = mData;
        this.mLayoutRes = mLayoutRes;
    }

    public void add(T data){
        if (mData == null){
            mData = new ArrayList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }
    public void add(int position ,T data){
        if (mData == null){
            mData = new ArrayList<>();
        }
        mData.add(position,data);
        notifyDataSetChanged();
    }
    public void remove(T data){
        if (mData == null){
            mData = new ArrayList<>();
        }
        mData.remove(data);
        notifyDataSetChanged();
    }
    public void remove(int position){
        if (mData == null){
            mData = new ArrayList<>();
        }
        mData.remove(position);
        notifyDataSetChanged();
    }
    public void removeAll(){
        if (mData == null){
            mData = new ArrayList<>();
        }
        mData.clear();
        notifyDataSetChanged();
    }

    public static class ViewHolder{
        private SparseArray mViews; //存储ListView的item的View
        private View item;          //存放convertView
        private int position;       //游标
        private Context context;    //Context上下文

        public ViewHolder(Context context,ViewGroup parent, int layoutRes){
            mViews = new SparseArray<>();
            this.context = context;
            View convertView = LayoutInflater.from(context).inflate(layoutRes,parent,false);
            convertView.setTag(this);
            item = convertView;
        }

        public static ViewHolder bind(Context context, View convertView ,ViewGroup parent,
                                      int layoutRes,int position){
            ViewHolder holder;
            if (convertView == null){
                holder = new ViewHolder(context,parent,layoutRes);
            }else {
                holder = (ViewHolder) convertView.getTag();
                holder.item = convertView;
            }
            holder.position = position;
            return holder;
        }

        @SuppressWarnings("unchecked")
        public <T extends View> T getView(int id){
            T t = (T)mViews.get(id);
            if (t == null){
                t = (T)item.findViewById(id);
                mViews.put(id ,t);
            }
            return t;
        }


        public View getItemView(){
            return item;
        }

        public int getItemPosition(){
            return position;
        }

        public ViewHolder setText(int id,CharSequence text){
            View view =getView(id);
            if (view instanceof TextView){
                ((TextView) view).setText(text);
            }
            return this;
        }
        public ViewHolder setListener(int id,View.OnClickListener listener){
            getView(id).setOnClickListener(listener);
            return this;
        }

        /**
         * 设置图片
         */
        public ViewHolder setImageBitmap(int id, String url , RequestQueue queue) {
            View view = getView(id);
            if (view instanceof ImageView) {
                ImageUtils.Loadimg(((ImageView) view),url,queue);
            }
            return this;
        }
    }
}
